package com.mdxq.mall.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mdxq.mall.user.entity.AdminLoginLog;
import com.mdxq.mall.user.entity.AdminRoleRelation;
import com.mdxq.mall.user.entity.table.AdminLoginLogTableDef;
import com.mdxq.mall.user.mapper.AdminLoginLogMapper;
import com.mdxq.mall.user.mapper.AdminRoleRelationMapper;
import com.mdxq.mall.user.service.AdminRoleRelationService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mdxq.mall.user.entity.Admin;
import com.mdxq.mall.user.mapper.AdminMapper;
import com.mdxq.mall.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户表 服务层实现。
 *
 * @author zxr
 * @since 2025-01-12
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>  implements AdminService{
    @Autowired
    //装配一个登录日志Mapper
    private AdminLoginLogMapper adminLoginLogMapper;


    /**
     * 管理员登录
     *
     * @param username 账号
     * @param password 密码
     * @return 登录成功返回管理员对像，失败返回null
     */
    @Override
    public Admin login(String username, String password, HttpServletRequest request) {

        QueryWrapper queryWrapper = new QueryWrapper();
        //                    表的用户名字段,客户端填写的用户名
        queryWrapper.eq("username",username);
        queryWrapper.eq("status",1);// 查询状态为1 （正常使用）的账号
        // 使用getOne方法需要保证查到的结果最多是一条
        Admin admin = this.getOne(queryWrapper);
        if(admin != null){ // 说明查到了这个账号且状态正常
            if(BCrypt.checkpw(password,admin.getPassword())){
//                UpdateWrapper updateWrapper = UpdateWrapper.of(Admin.class);
                admin.setLoginTime(LocalDateTime.now());
//                updateWrapper.set("loginTime",LocalDateTime.now());
                mapper.updateByQuery(admin,true,queryWrapper);
                //封装一个登录日志实例
                AdminLoginLog log = new AdminLoginLog();
                log.setAdminId(admin.getId());
                log.setIp(request.getRemoteHost());
                log.setAddress(request.getRemoteAddr());
                log.setCreateTime(LocalDateTime.now());
                log.setUserAgent(request.getHeader("user-agent"));
                adminLoginLogMapper.insert(log);
                return admin;
            }
        }
        return null;
    }

    @Autowired
    AdminRoleRelationMapper relationMapper;
    @Autowired
    AdminMapper adminMapper;
    @Override
    @Transactional
    public boolean allocRole(Long adminId, String roleIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_id",adminId);
        relationMapper.deleteByQuery(queryWrapper);
        if(StrUtil.isNotBlank(roleIds)){
            List<AdminRoleRelation>  relationList = new ArrayList<>();
            for (String roleId : roleIds.split(",")) {
                relationList.add(new AdminRoleRelation(null,adminId,Long.valueOf(roleId)));
            }
            relationMapper.insertBatch(relationList);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        }
        return true;
    }

    @Override
    public Admin getById(Serializable id) {
        Admin admin = super.getById(id);
        admin.setRoles(mapper.selectRolesByAdminId(admin.getId()));
        admin.setMenus(mapper.selectMenusByAdminId(admin.getId()));
        return admin;
    }
}
